package com.bestcem.xm.component.security.configure;

import com.bestcem.xm.component.security.interceptor.LoginInterceptor;
import com.bestcem.xm.component.security.properties.XmSecurityProperties;
import com.bestcem.xm.component.security.retry.CheckAuthenticationRetry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @ClassName: XmSecurityAutoConfigure
 * @Description:
 * @Author: chenglong.yue
 * @Date: 2022/6/6 16:23
 */
@Configuration
@EnableConfigurationProperties({XmSecurityProperties.class})
public class XmSecurityAutoConfigure {

    private XmSecurityProperties properties;

    private CheckAuthenticationRetry checkAuthenticationRetry;

    @Autowired
    public void setProperties(XmSecurityProperties properties) {
        this.properties = properties;
    }

    @Autowired
    public void setCheckAuthenticationRetry(CheckAuthenticationRetry checkAuthenticationRetry) {
        this.checkAuthenticationRetry = checkAuthenticationRetry;
    }


    private LoginInterceptor getLoginInterceptor(XmSecurityProperties properties, CheckAuthenticationRetry checkAuthenticationRetry) {
        LoginInterceptor interceptor = new LoginInterceptor();
        interceptor.setProperties(properties);
        interceptor.setCheckAuthenticationRetry(checkAuthenticationRetry);
        return interceptor;
    }

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnClass(com.bestcem.xm.component.security.interceptor.LoginInterceptor.class)
    public XmSecurityInterceptorConfigure xmSecurityInterceptorConfigure() {
        LoginInterceptor interceptor = getLoginInterceptor(properties, checkAuthenticationRetry);
        XmSecurityInterceptorConfigure configure = new XmSecurityInterceptorConfigure();
        configure.setLoginInterceptor(interceptor);
        return configure;
    }
}
